package com.shujia.service;

import com.shujia.bean.Student;
import com.shujia.dao.StudentDao;
import com.shujia.dao.StudentDaoImpl;

import java.util.HashMap;

public class StudentServiceImpl implements StudentService {

    private StudentDao studentDao = new StudentDaoImpl();


    //缓存
    //hashmap是临时测试方案，上线一般使用redis (内存数据库)
    HashMap<String, Student> cache = new HashMap<>();


    @Override
    public Student queryStudentById(String id, String key) {

        //判断key是否正确
        if (!"123456".equals(key)) {

            return null;
        }


        //通过学号查奥逊学生的信息
        Student student = studentDao.queryStudentById(id);
        return student;

    }

    @Override
    public Student queryStudentByIdAndCache(String id, String key) {


        //判断key是否正确
        if (!"123456".equals(key)) {

            return null;
        }

        //1、先查询缓存

        Student student = cache.get(id);

        if (student != null) {
            return student;
        }

        //2、如果缓存中没有再查询数据库

        Student mysqlStudent = studentDao.queryStudentById(id);


        //3、将查询到的学生保存到缓存中
        cache.put(id, mysqlStudent);


        return mysqlStudent;

    }
}
